package com.ruoyi.info.controller;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.AddressUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.info.domain.CmsComment;
import com.ruoyi.info.domain.CmsContent;
import com.ruoyi.info.domain.CmsInfo;
import com.ruoyi.info.service.ICmsCommentService;
import com.ruoyi.info.service.ICmsContentService;
import com.ruoyi.info.service.ICmsInfoService;

import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/web")
public class WebController extends BaseController {
    private String prefix = "web/";

    @Autowired
    private ICmsContentService cmsContentService;

    @Autowired
    private ICmsInfoService cmsInfoService;

    @Autowired
    private ICmsCommentService cmsCommentService;

    @Autowired
    private ISysUserService userService;


    @GetMapping("/news")
    public String news(ModelMap modelMap) {

        return prefix + "news";
    }

    @GetMapping("/safety")
    public String safety(ModelMap modelMap) {
        return prefix + "safety";
    }

    @GetMapping("/safetyAll")
    public String safetyAll(ModelMap modelMap) {
        return prefix + "safetyAll";
    }

    @GetMapping("/virusAll")
    public String virusAll(ModelMap modelMap) {
        return prefix + "virusAll";
    }

    @GetMapping("/forum")
    public String forum(ModelMap modelMap) {
        return prefix + "forum";
    }

    @GetMapping("/forumAapply")
    public String forumApply(ModelMap modelMap) {
        return prefix + "forumApply";
    }

    @GetMapping("/about")
    public String about(ModelMap modelMap) {
        return prefix + "about";
    }

    @GetMapping("/center")
    public String center(ModelMap modelMap) {
        return prefix + "center";
    }

    /**
     * 新闻详情
     *
     * @param uuid
     * @param mmap
     * @return
     */
    @GetMapping("/news/view/{uuid}")
    public String newsPreview(@PathVariable("uuid") String uuid, ModelMap mmap) {
        CmsContent cmsContent = cmsContentService.selectCmsContentByUuId(uuid);
        cmsContentService.setViewAdd(cmsContent);
        List<CmsComment> comments = cmsCommentService.getContentComments(cmsContent.getId());
        // 获取当前的用户信息
        SysUser currentUser = ShiroUtils.getSysUser();
        int commentSize = cmsCommentService.getCommentSize(cmsContent.getId());
        List<Long> likes = cmsContentService.getLikes(cmsContent.getId());

        mmap.put("cmsContent", cmsContent);
        mmap.put("comments", comments);
        mmap.put("currentUser", currentUser);
        mmap.put("commentSize", commentSize);
        mmap.put("likes", likes);
        return prefix + "newsView";
    }

    /**
     * 安全摘要
     *
     * @param uuid
     * @param mmap
     * @return
     */
    @GetMapping("/safety/view/{uuid}")
    public String safeView(@PathVariable("uuid") String uuid, ModelMap mmap) {
        CmsContent cmsContent = cmsContentService.selectCmsContentByUuId(uuid);
        cmsContentService.setViewAdd(cmsContent);
        List<CmsComment> comments = cmsCommentService.getContentComments(cmsContent.getId());
        int commentSize = cmsCommentService.getCommentSize(cmsContent.getId());
        // 获取当前的用户信息
        SysUser currentUser = ShiroUtils.getSysUser();

        List<Long> likes = cmsContentService.getLikes(cmsContent.getId());


        mmap.put("cmsContent", cmsContent);
        mmap.put("comments", comments);
        mmap.put("currentUser", currentUser);
        mmap.put("commentSize", commentSize);
        mmap.put("likes", likes);
        return prefix + "newsView";
    }

    @GetMapping("/safety/view2/{uuid}")
    public String safeView2(@PathVariable("uuid") String uuid, ModelMap mmap) {
        CmsContent cmsContent = cmsContentService.selectCmsContentByUuId(uuid);
        cmsContentService.setViewAdd(cmsContent);
        List<CmsComment> comments = cmsCommentService.getContentComments(cmsContent.getId());
        int commentSize = cmsCommentService.getCommentSize(cmsContent.getId());

        // 获取当前的用户信息
        SysUser currentUser = ShiroUtils.getSysUser();

        List<Long> likes = cmsContentService.getLikes(cmsContent.getId());

        mmap.put("cmsContent", cmsContent);
        mmap.put("comments", comments);
        mmap.put("currentUser", currentUser);
        mmap.put("commentSize", commentSize);
        mmap.put("likes", likes);
        return prefix + "virusView";
    }

    @GetMapping("/getUserInfo")
    @ResponseBody
    public AjaxResult getUserInfo() {
        SysUser currentUser = ShiroUtils.getSysUser();
        return AjaxResult.success(currentUser);
    }

    @PostMapping("/addComments")
    @ResponseBody
    public AjaxResult addComments(CmsComment cmsComment, String puid) {
        SysUser currentUser = ShiroUtils.getSysUser();
        String ip = ShiroUtils.getIp();
        String address = AddressUtils.getRealAddressByIP(ip);

        cmsComment.setUserId(currentUser.getUserId());
        cmsComment.setCreateTime(new Date());
        cmsComment.setIp(ip);
        cmsComment.setAddress(address);
        cmsComment.setStatus("0");

        cmsComment.setUserName(currentUser.getUserName());
        cmsComment.setAvatar(currentUser.getAvatar());

        if (!StringUtils.isEmpty(puid)) {
            SysUser puser = userService.selectUserById(Long.valueOf(puid));
            cmsComment.setParentAvatar(puser.getAvatar());
            cmsComment.setParentUserName(puser.getUserName());
        }

        cmsCommentService.insertCmsComment(cmsComment);
        return AjaxResult.success(cmsComment);
    }

    @GetMapping("/getComments/{id}")
    @ResponseBody
    public AjaxResult getComments(@PathVariable("id") Long id) {
        List<CmsComment> comments = cmsCommentService.getContentComments(id);
        return AjaxResult.success(comments);
    }

    @GetMapping("/getCommentSize/{id}")
    @ResponseBody
    public AjaxResult getCommentSize(@PathVariable("id") Long id) {
        int commentSize = cmsCommentService.getCommentSize(id);
        return AjaxResult.success(commentSize);
    }

    @PostMapping("/setContentLike")
    @ResponseBody
    public AjaxResult setContentLike(Long contentId) {
        SysUser currentUser = ShiroUtils.getSysUser();
        Long userId = currentUser.getUserId();

        cmsContentService.setcontentLike(contentId, userId);

        return AjaxResult.success();
    }

    @GetMapping("/getLikes")
    @ResponseBody
    public AjaxResult getLikes(Long contentId) {
        List<Long> likes = cmsContentService.getLikes(contentId);
        return AjaxResult.success(likes);
    }


    @GetMapping("/getInfo")
    @ResponseBody
    public AjaxResult getInfo(String keyCode) {
        CmsInfo info = cmsInfoService.getByKey(keyCode);
        return AjaxResult.success(info);
    }

    @GetMapping("/search")
    public String search(String seo, ModelMap mmap) throws Exception {
        seo = URLDecoder.decode(seo, "UTF-8");

        List<CmsContent> content = cmsContentService.search(seo);
        mmap.put("content", content);

        return prefix + "search";
    }

    @PostMapping("/loadContentPage")
    @ResponseBody
    public TableDataInfo loadContentPage(CmsContent content) {
        startPage();
        List<CmsContent> list = cmsContentService.selectCmsContentList(content);
        return getDataTable(list);
    }

}
